/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_MODELS_IDX_MAP_ITERATOR_TOOLS_HPP
#define _CUBEFS_CORE_MODELS_IDX_MAP_ITERATOR_TOOLS_HPP

#include <iostream>

#include <cubefs/api/types.hpp>
#include <cubefs/core/container/container.hpp>
#include <cubefs/core/models/idx_map/names.hpp>

#define CUBEFS_SOURCE "cubefs/core/models/idx_map/iterator/tools.hpp:" CUBEFS_AT

namespace cubefs    {
namespace ctr       {


CUBEFS_ITERATOR_PART_NO_CTOR_BEGIN(cubefs::models::idx_map::IteratorToolsName)

    typedef typename Base::NodeBase                                             NodeBase;
    typedef typename Base::Model                                                Model;
    typedef typename Model::Key                                                 Key;
    typedef typename Model::Value                                               Value;

    static const Int Indexes = Model::Indexes;

    Key prefix_[Indexes];

    IteratorPart(MyType &me): Base(me), me_(me)
    {
        for (Int c = 0; c < Indexes; c++) {
            prefix_[c] = 0;
        }
    }

    void setup(const MyType &other)
    {
        for (Int c = 0; c < Indexes; c++) {
            prefix_[c] = other.prefix_[c];
        }

        Base::setup(other);
    }

    Key& prefix(Int i) {
        return prefix_[i];
    }

    const Key& prefix(Int i) const {
        return prefix_[i];
    }

    Key GetRawKey(Int i) {
        return Base::GetKey(i);
    }

    Key GetKey(Int i) {
        return GetRawKey(i) + prefix_[i];
    }

    bool NextKey() {
        //CUBEFS_TRACE(me_, "NextKey");
        if (!me_.IsEof()) {
            for (Int c = 0; c < Indexes; c++) {
                prefix_[c] += GetRawKey(c);
            }
            return Base::NextKey();
        }
        else {
            return false;
        }
    }


    void ComputeBase() {
        if (!me_.IsEmpty()) {
            compute_base(prefix_, me_.page(), me_.key_idx());
        }
    }
//
//    bool NextLeaf() {
//        if(Base::NextLeaf()) {
//            ComputeBase();
//            return true;
//        }
//        return false;
//    }
//
//    bool PrevLeaf() {
//        if(Base::PrevLeaf()) {
//            ComputeBase();
//            return true;
//        }
//        return false;
//    }

    void Init() {
        //ComputeBase();
    }

private:
    
    void compute_base(Key* values, NodeBase* page, Int idx) {
        if (idx > 0) {
            me_.model().SumKeys(page, 0, idx, values);
        }
        
        if (!page->is_root())
        {
            Int parent_idx = page->parent_idx();
            NodeBase *parent = me_.model().GetParent(page);
            compute_base(values, parent, parent_idx);
        }
    }


CUBEFS_ITERATOR_PART_END

}
}


#undef CUBEFS_SOURCE
#endif
